package com.miguo.framework.view;

import android.widget.RelativeLayout;
import android.content.Context;
import android.widget.ImageView;
import android.util.AttributeSet;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation;
import android.view.LayoutInflater;

import com.miguo.framework.R;

public class LoadingView extends RelativeLayout {
    private boolean isDoingAnimation;
    private boolean isNeedStopAnimation;
    private Context mContext;
    private ImageView mPointImage1;
    private ImageView mPointImage2;
    private ImageView mPointImage3;
    private ImageView mPointImage4;
    private ImageView mPointImage5;
    private ImageView mTipText;

    public LoadingView(Context context) {
        super(context);
        mContext = context;
        initView();
    }

    public LoadingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initView();
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.layer_load_view, this);
        mPointImage1 = (ImageView) findViewById(R.id.img_1);
        mPointImage2 = (ImageView) findViewById(R.id.img_2);
        mPointImage3 = (ImageView) findViewById(R.id.img_3);
        mPointImage4 = (ImageView) findViewById(R.id.img_4);
        mPointImage5 = (ImageView) findViewById(R.id.img_5);
        mTipText = (ImageView) findViewById(R.id.tip_text);
    }

    public void isShowLoading(boolean isShow) {
        if (isShow) {
            isNeedStopAnimation = false;
            if (!isDoingAnimation) {
                isDoingAnimation = true;
                doAnimation();
            }
            return;
        }
        isNeedStopAnimation = true;
        isDoingAnimation = false;
        if (mPointImage1 != null) {
            mPointImage1.clearAnimation();
        }
        if (mPointImage2 != null) {
            mPointImage2.clearAnimation();
        }
        if (mPointImage3 != null) {
            mPointImage3.clearAnimation();
        }
        if (mPointImage4 != null) {
            mPointImage4.clearAnimation();
        }
        if (mPointImage5 != null) {
            mPointImage5.clearAnimation();
        }
        if (mTipText != null) {
            mTipText.clearAnimation();
        }
    }

    private Animation getAnimationResource() {
        Animation operatingAnim = AnimationUtils.loadAnimation(mContext, R.anim.load_view_rotate);
        operatingAnim.setFillAfter(true);
        return operatingAnim;
    }

    private void doAnimation() {
        Animation alphaAnim = AnimationUtils.loadAnimation(mContext, R.anim.load_view_text_alpha_in);
        alphaAnim.setFillAfter(true);
        mTipText.startAnimation(alphaAnim);
        alphaAnim.setAnimationListener(new Animation.AnimationListener() {

            public void onAnimationStart(Animation animation) {
            }

            public void onAnimationEnd(Animation animation) {
                Animation alphaAnim = AnimationUtils.loadAnimation(mContext, R.anim.load_view_text_alpha_out);
                mTipText.startAnimation(alphaAnim);
            }

            public void onAnimationRepeat(Animation animation) {
            }
        });
        Animation rotateAnimation = getAnimationResource();
        mPointImage1.startAnimation(rotateAnimation);
        rotateAnimation = getAnimationResource();
        rotateAnimation.setStartOffset(100L);
        mPointImage2.startAnimation(rotateAnimation);
        rotateAnimation = getAnimationResource();
        rotateAnimation.setStartOffset(200L);
        mPointImage3.startAnimation(rotateAnimation);
        rotateAnimation = getAnimationResource();
        rotateAnimation.setStartOffset(300L);
        mPointImage4.startAnimation(rotateAnimation);
        rotateAnimation = getAnimationResource();
        rotateAnimation.setStartOffset(400L);
        mPointImage5.startAnimation(rotateAnimation);
        rotateAnimation.setAnimationListener(new Animation.AnimationListener() {

            public void onAnimationStart(Animation animation) {
            }

            public void onAnimationEnd(Animation animation) {
                if (!isNeedStopAnimation) {
                    doAnimation();
                }
            }

            public void onAnimationRepeat(Animation animation) {
            }
        });
    }
}

